package ms.jacrim.pddl.viewer;

import java.io.File;
import java.io.FileNotFoundException;

import ms.spm.IAppContext;

import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.CTabFolder;
import org.eclipse.swt.custom.CTabItem;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Dialog;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;

import com.swtdesigner.SWTResourceManager;

public class SolutionViewerForm extends Dialog {

	private CTabFolder tabFolder;
	private IAppContext context;
	
	protected Object result;
	protected Shell shell;

	/**
	 * Create the dialog
	 * 
	 * @param parent
	 * @param style
	 */
	public SolutionViewerForm(Shell parent, int style) {
		super(parent, style);
	}

	/**
	 * Create the dialog
	 * 
	 * @param parent
	 */
	public SolutionViewerForm(Shell parent) {
		this(parent, SWT.NONE);
	}

	/**
	 * Open the dialog
	 * 
	 * @return the result
	 * @throws FileNotFoundException
	 */
	public Object open(String... files) throws Exception {
		createContents();
		if (files != null)
			for (int i = 1; i < files.length; i++) {
				String file = files[i];
				CTabItem item = new CTabItem(tabFolder, SWT.NONE);
				SolutionViewer sv = new SolutionViewer(context, tabFolder, SWT.NONE);
				item.setText(new File(file).getName());
				item.setToolTipText(file);
				item.setControl(sv);
				item.setImage(SWTResourceManager.getImage(SolutionViewer.class, "solution_document.png"));
				sv.loadSolution(file, files[0]);
			}
		shell.open();
		shell.layout();
		Display display = getParent().getDisplay();
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch())
				display.sleep();
		}
		return result;
	}
	

	/**
	 * Create contents of the dialog
	 */
	protected void createContents() {
		shell = new Shell(getParent(), SWT.APPLICATION_MODAL | SWT.TITLE | SWT.BORDER | SWT.RESIZE | SWT.CLOSE | SWT.MAX);
		shell.setLayout(new FormLayout());
		shell.setText("Solution Viewer");

		tabFolder = new CTabFolder(shell, SWT.BORDER);
		final FormData fd_tabFolder = new FormData();
		fd_tabFolder.top = new FormAttachment(0, 5);
		fd_tabFolder.right = new FormAttachment(100, -5);
		fd_tabFolder.left = new FormAttachment(0, 5);
		tabFolder.setLayoutData(fd_tabFolder);
		
		Button okButton;
		okButton = new Button(shell, SWT.PUSH);
		okButton.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(final SelectionEvent arg0) {
				shell.dispose();
			}
		});
		fd_tabFolder.bottom = new FormAttachment(okButton, -5, SWT.TOP);
		final FormData fd_okButton = new FormData();
		fd_okButton.left = new FormAttachment(100, -104);
		fd_okButton.bottom = new FormAttachment(100, -7);
		fd_okButton.top = new FormAttachment(100, -32);
		fd_okButton.right = new FormAttachment(100, -11);
		okButton.setLayoutData(fd_okButton);
		okButton.setText("Close");
		//
	}

}
